import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'

interface BasicSettings {
  systemName: string
  systemDescription: string
  defaultLanguage: string
  timezone: string
}

interface StorageSettings {
  storageType: string
  storagePath: string
  maxFileSize: number
  allowedTypes: string[]
}

interface PlayerSettings {
  defaultVolume: number
  autoPlay: boolean
  loop: boolean
  speedOptions: string[]
  qualityOptions: string[]
}

interface SecuritySettings {
  enableAccessControl: boolean
  allowAnonymous: boolean
  sessionTimeout: number
  passwordPolicy: string[]
}

interface NotificationSettings {
  emailEnabled: boolean
  smtpServer: string
  smtpPort: number
  senderEmail: string
  events: string[]
}

export const useSettingsStore = defineStore('settings', () => {
  const loading = ref(false)

  // 基本设置
  const basicSettings = reactive<BasicSettings>({
    systemName: '音视频管理系统',
    systemDescription: '专业的音视频文件管理平台',
    defaultLanguage: 'zh-CN',
    timezone: 'Asia/Shanghai'
  })

  // 存储设置
  const storageSettings = reactive<StorageSettings>({
    storageType: 'local',
    storagePath: '/uploads',
    maxFileSize: 500,
    allowedTypes: ['mp4', 'avi', 'mp3', 'wav']
  })

  // 播放设置
  const playerSettings = reactive<PlayerSettings>({
    defaultVolume: 80,
    autoPlay: false,
    loop: false,
    speedOptions: ['0.5', '0.75', '1.0', '1.25', '1.5', '2.0'],
    qualityOptions: ['360p', '480p', '720p', '1080p']
  })

  // 安全设置
  const securitySettings = reactive<SecuritySettings>({
    enableAccessControl: true,
    allowAnonymous: false,
    sessionTimeout: 30,
    passwordPolicy: ['minLength', 'requireNumber', 'requireLetter']
  })

  // 通知设置
  const notificationSettings = reactive<NotificationSettings>({
    emailEnabled: false,
    smtpServer: '',
    smtpPort: 587,
    senderEmail: '',
    events: ['upload', 'error']
  })

  // 保存设置
  const saveSettings = async () => {
    loading.value = true
    try {
      // 模拟保存API调用
      await new Promise(resolve => setTimeout(resolve, 1000))
      
      // 保存到本地存储
      localStorage.setItem('settings', JSON.stringify({
        basic: basicSettings,
        storage: storageSettings,
        player: playerSettings,
        security: securitySettings,
        notification: notificationSettings
      }))
      
      return true
    } finally {
      loading.value = false
    }
  }

  // 加载设置
  const loadSettings = () => {
    try {
      const savedSettings = localStorage.getItem('settings')
      if (savedSettings) {
        const settings = JSON.parse(savedSettings)
        
        if (settings.basic) Object.assign(basicSettings, settings.basic)
        if (settings.storage) Object.assign(storageSettings, settings.storage)
        if (settings.player) Object.assign(playerSettings, settings.player)
        if (settings.security) Object.assign(securitySettings, settings.security)
        if (settings.notification) Object.assign(notificationSettings, settings.notification)
      }
    } catch (error) {
      console.error('加载设置失败:', error)
    }
  }

  // 重置设置
  const resetSettings = () => {
    Object.assign(basicSettings, {
      systemName: '音视频管理系统',
      systemDescription: '专业的音视频文件管理平台',
      defaultLanguage: 'zh-CN',
      timezone: 'Asia/Shanghai'
    })
    
    Object.assign(storageSettings, {
      storageType: 'local',
      storagePath: '/uploads',
      maxFileSize: 500,
      allowedTypes: ['mp4', 'avi', 'mp3', 'wav']
    })
    
    Object.assign(playerSettings, {
      defaultVolume: 80,
      autoPlay: false,
      loop: false,
      speedOptions: ['0.5', '0.75', '1.0', '1.25', '1.5', '2.0'],
      qualityOptions: ['360p', '480p', '720p', '1080p']
    })
    
    Object.assign(securitySettings, {
      enableAccessControl: true,
      allowAnonymous: false,
      sessionTimeout: 30,
      passwordPolicy: ['minLength', 'requireNumber', 'requireLetter']
    })
    
    Object.assign(notificationSettings, {
      emailEnabled: false,
      smtpServer: '',
      smtpPort: 587,
      senderEmail: '',
      events: ['upload', 'error']
    })
  }

  // 导出配置
  const exportSettings = () => {
    const config = {
      basic: basicSettings,
      storage: storageSettings,
      player: playerSettings,
      security: securitySettings,
      notification: notificationSettings
    }
    
    const blob = new Blob([JSON.stringify(config, null, 2)], { type: 'application/json' })
    const url = URL.createObjectURL(blob)
    const link = document.createElement('a')
    link.href = url
    link.download = 'media-system-config.json'
    link.click()
    URL.revokeObjectURL(url)
  }

  return {
    loading,
    basicSettings,
    storageSettings,
    playerSettings,
    securitySettings,
    notificationSettings,
    saveSettings,
    loadSettings,
    resetSettings,
    exportSettings
  }
}) 